package com.listener;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: Publiser
 * @Description: 发布者
 * @Author Fly
 * @Date 2022/12/19 11:23
 */
public class EventManager {
    Map<String, List<EventListener>> listeners = new HashMap<String, List<EventListener>>();

    public EventManager(String... operations) {
        for (String operation : operations) {
            listeners.put(operation, new ArrayList<EventListener>());
        }
    }

    public void subscribe(String eventType, EventListener listener) {
        List<EventListener> eventListeners = listeners.get(eventType);
        eventListeners.add(listener);
    }

    public void unsubscribe(String eventType, EventListener listener) {
        List<EventListener> eventListeners = listeners.get(eventType);
        eventListeners.remove(listener);
    }

    public void notify(String eventType, File file) {
        List<EventListener> listeners = this.listeners.get(eventType);
        for (EventListener listener : listeners) {
            listener.update(eventType, file);
        }
    }


}
